package com.wn.icloud.common.utils;

import java.util.*;

/**
 * @Author: liguangcai
 * @Date: 2018/12/28 0028 11:24
 */
public class ColorUtil {

    // 不允许用的颜色
    public static List<String> notAllowed = Arrays.asList("#000000", "#FFFF00", "#FF0000", "#FFFFFF", "#FFFAF0", "#778899");
    // 优先使用的颜色
    public static List<String> recomdColors = Arrays.asList("#436EEE", "#EE0000", "#218868", "#FF4500", "#B3EE3A", "#97FFFF", "#87CEFA", "#66CD00", "#E066FF");
//    #FF4500， #B3EE3A  #97FFFF  #87CEFA	#66CD00  #E066FF

    /**
     * 生成随机颜色
     *
     * @return
     */
    public static String getColor() {
        String r, g, b;
        Random random = new Random();
        r = Integer.toHexString(random.nextInt(256)).toUpperCase();
        g = Integer.toHexString(random.nextInt(256)).toUpperCase();
        b = Integer.toHexString(random.nextInt(256)).toUpperCase();
        r = r.length() == 1 ? "0" + r : r;
        g = g.length() == 1 ? "0" + g : g;
        b = b.length() == 1 ? "0" + b : b;
        String color = "#" + r + g + b;
        if (!notAllowed.contains(color)) {
            return color;
        }
        return getColor();
    }

    /**
     * 随机生成num种不同的颜色
     * @param num
     * @return
     */
    public static List<String> getColor(int num) {
        HashSet<String> set = new HashSet<>();
        if (num < recomdColors.size()) {
            return recomdColors.subList(0, num);
        } else {
            do {
                String color = getColor();
                set.add(color);
            } while (set.size() <= num);
        }
        return new ArrayList<>(set);
    }

    /**
     * 随机生成对应的颜色
     * @param rootIds
     * @return
     */
    public static Map<String, String> getColor(List<String> rootIds) {
        Map<String, String> map = new HashMap<>();
        List<String> list = getColor(rootIds.size());
        for (int i = 0; i < list.size(); i++) {
            map.put(rootIds.get(i), list.get(i));
        }
        return map;
    }
}
